# 使用 Miniconda 基础镜像
FROM continuumio/miniconda3:latest

# 切换到 root 用户进行安装
USER root

# 安装系统工具（使用默认 Debian 官方源）
# 配置 apt 使用阿里云镜像源并安装系统工具（修正 security 源）
RUN echo "deb https://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list && \
    echo "deb https://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.aliyun.com/debian-security/ bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list && \
    rm -f /etc/apt/sources.list.d/* && \
    apt-get update && apt-get install -y \
    vim \
    git \
    curl \
    wget \
    build-essential \
    openssh-client \
    && rm -rf /var/lib/apt/lists/*

# 安装 Git LFS
RUN curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash && \
    apt-get update && apt-get install -y git-lfs && \
    git lfs install && \
    rm -rf /var/lib/apt/lists/*

# 配置 Git 用户信息
RUN git config --global user.email "gaoyu.ssdut@icloud.com" && \
    git config --global user.name "gaoyussdut"

# 创建 SSH 目录并添加密钥
RUN mkdir -p /root/.ssh && chmod 700 /root/.ssh

# 添加 SSH 私钥
RUN echo '-----BEGIN OPENSSH PRIVATE KEY-----' > /root/.ssh/id_ed25519 && \
    echo 'b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW' >> /root/.ssh/id_ed25519 && \
    echo 'QyNTUxOQAAACA9wMmnFncu1tMwTqqF3mjiYpE/wJKaDd+RWp/tTPA1UQAAAKDEvxagxL8W' >> /root/.ssh/id_ed25519 && \
    echo 'oAAAAAtzc2gtZWQyNTUxOQAAACA9wMmnFncu1tMwTqqF3mjiYpE/wJKaDd+RWp/tTPA1UQ' >> /root/.ssh/id_ed25519 && \
    echo 'AAAEA/A6uuTXUkUbwkOQq+yprYXlnfGNzrmN2IWoQpoO7Ouz3AyacWdy7W0zBOqoXeaOJi' >> /root/.ssh/id_ed25519 && \
    echo 'kT/AkpoN35Fan+1M8DVRAAAAFmdhb3l1LnNzZHV0QGljbG91ZC5jb20BAgMEBQYH' >> /root/.ssh/id_ed25519 && \
    echo '-----END OPENSSH PRIVATE KEY-----' >> /root/.ssh/id_ed25519 && \
    chmod 600 /root/.ssh/id_ed25519

# 添加 SSH 公钥
RUN echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID3AyacWdy7W0zBOqoXeaOJikT/AkpoN35Fan+1M8DVR gaoyu.ssdut@icloud.com' > /root/.ssh/id_ed25519.pub && \
    chmod 644 /root/.ssh/id_ed25519.pub

# 配置 SSH 客户端
RUN echo 'Host *' > /root/.ssh/config && \
    echo '    StrictHostKeyChecking no' >> /root/.ssh/config && \
    echo '    UserKnownHostsFile=/dev/null' >> /root/.ssh/config && \
    chmod 600 /root/.ssh/config

# 配置 conda 使用清华镜像源
RUN conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main && \
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free && \
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge && \
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch && \
    conda config --set show_channel_urls yes && \
    conda config --set channel_priority strict

# 创建 Python 3.11 环境
RUN conda create -n py311_reberto python=3.11 -y && \
    conda clean --all --yes

# 激活环境并安装气象包（全部使用清华镜像源）
RUN conda install -n py311_reberto \
    eccodes \
    cfgrib \
    pynio \
    gdal \
    pygrib \
    netcdf4 \
    xarray \
    numpy \
    scipy \
    pandas \
    -y && \
    conda clean --all --yes

# 配置 pip 使用清华镜像源并安装 Python 包（包括机器学习和NLP包）
RUN /opt/conda/envs/py311_reberto/bin/pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple && \
    /opt/conda/envs/py311_reberto/bin/pip install \
    fastapi \
    uvicorn[standard] \
    s3fs \
    requests \
    pydantic \
    python-multipart \
    torch>=2.2.0 \
    torchvision>=0.16.0 \
    torchaudio>=2.1.0 \
    transformers==4.35.2 \
    sentence-transformers==2.7.0 \
    "huggingface-hub>=0.30.0,<1.0" \
    faiss-cpu>=1.7.0

# 设置环境变量
ENV PATH="/opt/conda/envs/py311_reberto/bin:$PATH"
ENV CONDA_DEFAULT_ENV=py311_reberto

# 设置工作目录
WORKDIR /app

# 复制项目文件
COPY . /app

# 暴露端口
EXPOSE 8090

# 默认使用 bash 作为入口点，不自动启动应用
CMD ["/bin/bash"]

# 125服务器上构建镜像
# docker build -f Dockerfile.conda -t py311_reberto:conda .

# 导出镜像为 tar 文件
# docker save -o py311_reberto-conda.tar py311_reberto:conda

# 传输 tar 文件
# scp py311_reberto-conda.tar water@10.48.0.86:/home/water/docker/wenyu/

# 导入镜像
# docker load -i py311_reberto-conda.tar

# 查看导入的镜像
# docker images | grep py311_reberto

# 打 latest 标签
# docker tag py311_reberto:conda 172.20.7.86:5001/conda/py311-reberto:latest

# 登录 Nexus 仓库
# docker login 172.20.7.86:5001
# admin
# admin123

# 3. 推送镜像
# docker push 172.20.7.86:5001/conda/py311-reberto:latest

# 检查镜像仓库中的标签列表
# curl -u admin:admin123 -X GET "http://172.20.7.86:5001/v2/conda/py311-reberto/tags/list"
# 获取 latest 标签的镜像清单
# curl -u admin:admin123 -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -X GET "http://172.20.7.86:5001/v2/conda/py311-reberto/manifests/latest"
# 检查仓库目录
# curl -u admin:admin123 -X GET "http://172.20.7.86:5001/v2/_catalog"
# 获取镜像的详细配置信息
# curl -u admin:admin123 -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -X GET "http://172.20.7.86:5001/v2/conda/py311-reberto/manifests/latest" | jq .
# 如果您推送了多个标签，可以检查特定标签
# curl -u admin:admin123 -X GET "http://172.20.7.86:5001/v2/conda/py311-reberto/manifests/v1.0.0"
# 快速检查镜像是否存在（只返回状态码）
# curl -u admin:admin123 -I "http://172.20.7.86:5001/v2/conda/py311-reberto/manifests/latest"

# 4. 从其他机器拉取镜像
# docker pull 172.20.7.86:5001/conda/py311-reberto:latest

